/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "FarePickerVC.h"
#import "Passenger.h"
#import "CabDriver.h"

#define FarePickerVCCellHeight 60.0f

@implementation FarePickerVC

enum AlertViewButtons
{
	AlertViewCancelButton,
	AlertViewOkButton
};

- (id)initWithStyle:(UITableViewStyle)style 
{
	if (self = [super initWithStyle:style]) 
	{
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newPassengers) name:@"NewPassengers" object:nil];
		self.title = @"Available Fares";
	}
	return self;
}

- (void)viewWillAppear:(BOOL)animated
{
	passengerList = [[Passenger allPassengers] retain];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	return [passengerList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}
	cell.textLabel.text = [[passengerList objectAtIndex:indexPath.row] name];
	cell.detailTextLabel.text = [[passengerList objectAtIndex:indexPath.row] address];
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	[[[UIApplication sharedApplication] delegate] setAssignedPassenger:[[passengerList objectAtIndex:indexPath.row] retain]];
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Accept this fare?"
																		 message:nil
																		delegate:self
															cancelButtonTitle:@"Cancel"
															otherButtonTitles:@"Accept",nil];
	[alertView show]; 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return FarePickerVCCellHeight;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	Passenger* passenger = [[[UIApplication sharedApplication] delegate] assignedPassenger];
	CabDriver* cabDriver = [[[UIApplication sharedApplication] delegate] cabDriver];
	if(buttonIndex==AlertViewCancelButton)
	{
		[[[UIApplication sharedApplication] delegate] setAssignedPassenger:nil];
		return;
	}

	UIAlertView* alertView2 = [[UIAlertView alloc] initWithTitle:@"Passenger has been assigned"
																		 message:nil
																		delegate:nil
															cancelButtonTitle:@"Ok"
															otherButtonTitles:nil];
	[alertView2	show];
	
	[passenger setCabId:[NSString stringWithFormat:@"%d",[cabDriver ID]]];
	[passenger setStatus:@"Driver Assigned"];
	[passenger save];
	[[[UIApplication sharedApplication] delegate] setAssignedPassenger:passenger];
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)newPassengers
{
	[self.tableView reloadData];
}

@end
